import formJs from "./regexDetail.js"; //新增弹框配置项
// import publicForm from "@/components/public/up-form.vue"; //弹框form  照片必填用
import publicForm from "@/components/public/el-forms.vue"; //弹框form
import CustomDialog from "@/components/stationProductionPage/HntManage/pouringOrder/WBSHuitanGangbao/index.vue";
import CustomDialog1 from "@/components/PhysicalQualityPage/rebarThicknessManage/rebarEntrustList/index.vue";//委托单列表页

import {
    getSteelProtectionTestTaskDetail, //详情
    saveSteelProtectionTestTask, //保存
    getCheckitemsList, //试验检测依据-获取试验依据，检测依据
    getMangEquipmentPageList, //主要仪器设备名称及编号
    getUserCompanyTreeList,//机构树
    getBaseSgCompanyList,//机构树---只查施工单位
}
    from "@/api/request.js";
export default {
    props: [
        "dialogVisible",
        "dialogTitle",
        "statusNum",
        "currentRow",
    ],
    data() {
        return {
            visible: "",
            loading: false,
            title: "",
            isLock: false, //保存按钮是否可编辑
            dialogHeight: null,
            formData: {
                fCompanyname: "",
                fCompanyid: "",
                fId: "", // 申请单fid //为空判断为新增
                fWitnessid: "", //委托单id
                fWitnessno: "",//委托单编号
                fEngineeringname: "", // 工程名称
                fEngineeringid: "", // 工程id
                fEngineeringpart: "", // 工程部位
                fEngineeringpartid: "", // 工程部位id
                fPlantestdates: "", // 计划检测日期 年-月-日
                fTestcondition: "", // 试验条件
                fTestbasisid: "", // 检测依据id
                fTestbasisname: "", // 检测依据内容
                fTestbasisnameList: [], // 检测依据内容--页面展示
                fDecidebasisid: "", // 判定依据id
                fDecidebasisname: "", // 判定依据内容
                fDecidebasisnameList: [], // 判定依据内容--页面展示
                fEquipnameandnoid: "", // 检测仪器设备名称及编号id
                fEquipnameandno: "", // 检测仪器设备名称及编号
                fEquipnameandnoList: [], // 检测仪器设备名称及编号--页面展示
                fThicknessdesignvalue: "", // 保护层厚度设计值(mm)
                fAllowdeviation: "", // 允许偏差(+mm)
                fAllowdeviation1: "", // 允许偏差(-mm)
                fCorrectionfactor: "", // 修正因子(mm)
                fAuthorid: "", // 编制人ID
                fAuthorname: "", // 编制人名称
            },
            formObj: formJs.formObj, //报验信息
            examine: false, //专监审核是否展示
            Faudit: formJs.Faudit, //专监审核
            btnShow: true,
            newRow: null,
            timeYMD: null, //当前日期
            timeYmdHms: null,
            selected: [], //多选接值

            // WBS
            WBSShow: false,
            // WBS

            // 委托单列表页
            isShow: false,
            // 委托单列表页

        };
    },
    components: {
        publicForm,
        CustomDialog,
        CustomDialog1
    },
    watch: {
        dialogVisible: {
            handler(newVal) {
                if (newVal) {
                    this.visible = newVal;
                }
            },
            immediate: true,
        },

        dialogTitle: {
            handler(newVal) {
                if (newVal) {
                    this.title = newVal;
                }
            },
            immediate: true,
        },

        statusNum: {
            handler(newVal) {
                if (newVal) {
                    this.edit();
                }
            },
            immediate: true,
        },

        currentRow: {
            handler(newVal) {
                this.newRow = newVal;
            },
            deep: true,
            immediate: true,
        },

    },
    created() {
        window.addEventListener("resize", this.getHeight);
        this.getHeight();
    }, //获取页面高度自适应：

    destroyed() {
        window.removeEventListener("resize", this.getHeight);
        this.getHeight();
    },
    mounted() {
        this.userInfo = JSON.parse(sessionStorage.getItem("yjSmart-userInfo"));
        // console.log("userInfo", this.userInfo.companyNature);
        this.getCheckitemsList("5"); //钢保试验依据
        this.getCheckitemsList("6"); //钢保判定依据
        this.getEquipment() //主要仪器设备名称及编号
        this.getData();
        setTimeout(() => {
            var that = this;
            that.getListData();
        }, 100);

    },
    methods: {
        getHeight() {
            var h = document.documentElement.clientHeight; //获取页面可见高度
            this.dialogHeight = h * 0.5 + "px";
        },
        getTree() {
            var data = {
                companyId: this.userInfo.companyId,
                userId: this.userInfo.userId
            }
            this.formObj[1].options = [];
            getUserCompanyTreeList(data)
                .then((res) => {
                    if (res.errcode == 0) {
                        var companyTree = res.data.userCompanyTreeList;
                        var info = this.commonJs.tranListToTreeData(companyTree)[0].children;
                        info.map(item => {
                            this.formObj[1].options.push({
                                label: item.fShortname,
                                value: item.fCompanyid
                            })
                        })
                        this.formData.fCompanyname = this.formObj[1].options[0].label;
                        this.formData.fCompanyid = this.formObj[1].options[0].value;
                        // 列表
                    } else {
                        this.$message.error(res.msg);
                        return false;
                    }
                })
                .catch(() => {
                    return false;
                });
        },
        //中心试验室---查施工单位
        getBaseTree() {
            var data = {
                companyId: this.userInfo.companyId,
                userId: this.userInfo.userId
            }
            this.formObj[1].options = [];
            getBaseSgCompanyList(data)
                .then((res) => {
                    if (res.errcode == 0) {
                        var data = res.data.userCompanyTreeList;
                        data.map(item => {
                            this.formObj[1].options.push({
                                label: item.fShortname,
                                value: item.fCompanyid
                            })
                        })
                        this.formData.fCompanyname = this.formObj[1].options[0].label;
                        this.formData.fCompanyid = this.formObj[1].options[0].value;
                        // 列表
                    } else {
                        this.$message.error(res.msg);
                        return false;
                    }
                })
                .catch(() => {
                    return false;
                });
        },

        closeDialog() {
            this.$emit("close");
        },
        edit() {
            this.formData = this.$options.data.call(this).formData;
        },
        getListData() {
            this.btnShow = true;
            this.formObj.map(item => {
                item.disabled = false;
            });
            this.formObj[3].inputButton = true;
            this.formObj[0].disabled = true;
            if (this.statusNum == 1) {
                this.formObj[1].disabled = true;
                if (this.userInfo.companyNature == "施工单位") {
                    this.formObj[1].disabled = true;
                    
                    this.formData.fCompanyname = this.userInfo.companyFullName;
                    this.formData.fCompanyid = this.userInfo.companyId;
                } else if (this.userInfo.companyNature == "总监办") {
                    this.formObj[1].disabled = false;
                    this.getTree();
                } else if (this.userInfo.companyNature == "中心试验室") {
                    this.formObj[0].disabled = false;
                    this.formObj[1].disabled = false;
                    this.getBaseTree();
                }
                this.formObj[2].disabled = true;
                this.formObj[13].disabled = true;
                this.formData.fEngineeringname = this.userInfo.companyFullName;
                this.formData.fEngineeringid = this.userInfo.companyId;
                this.formData.fPlantestdates = this.timeYMD;
                this.formData.fAuthorid = this.userInfo.userId;
                this.formData.fAuthorname = this.userInfo.realName;
            } else if (this.statusNum == 3) {
                this.formObj[2].disabled = true;
                this.formObj[13].disabled = true;
                if (this.userInfo.companyNature == "施工单位") {
                    this.formObj[1].disabled = true;
                    this.formData.fCompanyname = this.userInfo.companyFullName;
                    this.formData.fCompanyid = this.userInfo.companyId;
                } else if (this.userInfo.companyNature == "总监办") {
                    this.formObj[1].disabled = false;
                    this.getTree();
                } else if (this.userInfo.companyNature == "中心试验室") {
                    this.formObj[0].disabled = false;
                    this.formObj[1].disabled = false;
                    this.getBaseTree();
                }
                this.getDetail(this.newRow);
            } else if (this.statusNum == 7) {
                this.btnShow = false;
                this.formObj.map(item => {
                    item.disabled = true;
                })
                this.formObj[3].inputButton = false;
                this.getDetail(this.newRow);
            }
        },
        // 详情
        getDetail(row) {
            this.loading = true;
            getSteelProtectionTestTaskDetail({
                fId: row.fId
            }).then((res) => {
                if (res.errcode == 0) {
                    var that = this;
                    that.loading = false;
                    that.formData = res.data.testTask;
                    if (that.statusNum == 3) {
                        //工程名称
                        if (that.formData.fEngineeringname == null || that.formData.fEngineeringid == null || that.formData.fEngineeringname == "" || that.formData.fEngineeringid == "") {
                            that.formData.fEngineeringid = that.userInfo.companyId;
                            that.formData.fEngineeringname = that.userInfo.companyFullName;
                        }
                        //计划检测日期
                        if (that.formData.fPlantestdates == null) {
                            that.formData.fPlantestdates = that.timeYMD;
                        }
                        //编制人名称
                        if (that.formData.fAuthorid == null || that.formData.fAuthorname == null || that.formData.fAuthorid == "" || that.formData.fAuthorname == "") {
                            that.formData.fAuthorname = that.userInfo.realName;
                            that.formData.fAuthorid = that.userInfo.userId;
                        }
                    }

                    // 多选
                    // 检测依据
                    if (res.data.testTask.fTestbasisname != '') {
                        var fTestnote = []
                        this.formObj[6].options.map(item => {
                            res.data.testTask.fTestbasisname.split(",").map(i => {
                                if (item.value == i) {
                                    fTestnote.push(item)
                                }
                            })
                        })
                    } else {
                        fTestnote = []
                    }
                    // 判定依据
                    if (res.data.testTask.fDecidebasisname != '') {
                        var fJudgetype = []
                        this.formObj[7].options.map(item => {
                            res.data.testTask.fDecidebasisname.split(",").map(i => {
                                if (item.value == i) {
                                    fJudgetype.push(item)
                                }
                            })
                        })
                    } else {
                        fJudgetype = []
                    }
                    // 设备
                    if (res.data.testTask.fEquipnameandnoid != '') {
                        var fEquipmentname = []
                        this.formObj[8].options.map(item => {
                            res.data.testTask.fEquipnameandno.split(",").map(i => {
                                if (item.label == i) {
                                    fEquipmentname.push(item);
                                }
                            })
                        })
                    } else {
                        fEquipmentname = []
                    }

                    //设备
                    that.selected = [{
                        name: "fTestbasisnameList",
                        selected: fTestnote,
                    },
                    {
                        name: "fDecidebasisnameList",
                        selected: fJudgetype,
                    },
                    {
                        name: "fEquipnameandnoList",
                        selected: fEquipmentname,
                    },
                    ];
                    // 多选
                } else {
                    this.loading = false;
                    this.$message.error(res.msg);
                }
            });
        },
        selectChange(data, a, b) {
            // console.log("selectChange", data, a, b);
            if (b == 'fTestbasisnameList') {
                var arr = [],
                    arrid = []
                data.map(item => {
                    arr.push(item.value);
                    arrid.push(item.label)
                })
                this.formData.fTestbasisname = arr.join(",");
                this.formData.fTestbasisid = arrid.join(",");
            }
            if (b == 'fDecidebasisnameList') {
                var arr1 = [],
                    arr1id = [];
                data.map(item => {
                    arr1.push(item.value);
                    arr1id.push(item.label);
                })
                this.formData.fDecidebasisname = arr1.join(",");
                this.formData.fDecidebasisid = arr1id.join(",");
            }
            if (b == 'fEquipnameandnoList') {
                var arr2 = [],
                    arr2id = [];
                data.map(item => {
                    arr2.push(item.label);
                    arr2id.push(item.value);
                })
                this.formData.fEquipnameandno = arr2.join(",");
                this.formData.fEquipnameandnoid = arr2id.join(",");
            }
            if (b == 'fCompanyname') {
                this.formData.fCompanyname = data.label;
                this.formData.fCompanyid = data.value;
                this.formData.fEngineeringpart = ""
                this.formData.fEngineeringpartid = ""
            }


        },
        inputFocus(index, prop) {
            // console.log("inputFocus", index, prop);
            if (prop == "fWitnessno") {
                this.isShow = true;
            }
            return index, prop;
        },
        clickDialog(index, prop) {
            // WBS树展示
            if (prop == "fEngineeringpart") {
                if (this.formData.fCompanyname != "" && this.formData.fCompanyname != null) {
                    this.WBSShow = true;
                } else {
                    this.$message.warning("请先选择施工单位");
                }

            }
            return index, prop;
        },
        // 试验检测依据-获取试验依据，检测依据
        getCheckitemsList(num) {
            getCheckitemsList({
                modelType: num,
            })
                .then((res) => {
                    if (res.errcode == 0) {
                        if (num == 5) { //回弹试验依据
                            this.formObj[6].options = [];
                            res.data.checkitemsList.map(item => {
                                this.formObj[6].options.push({
                                    label: item.fItemno,
                                    value: item.fItemname,
                                    fId: item.fId

                                })
                            })
                        } else if (num == 6) { //回弹判定依据
                            this.formObj[7].options = [];
                            res.data.checkitemsList.map(item => {
                                this.formObj[7].options.push({
                                    label: item.fItemno,
                                    value: item.fItemname,
                                    fId: item.fId

                                })
                            })

                        }

                    } else {
                        this.$message.error(res.msg);
                        return false;
                    }
                })
                .catch(() => {
                    return false;
                });
        },
        // 主要仪器设备名称及编号
        getEquipment() {
            getMangEquipmentPageList({
                // fUserid: this.userInfo.userId,
                fCompanyid: this.userInfo.companyId, //当前登录用户机构id
                keyWord: "", //设备名称模糊查询字段
                keyType: "钢筋保护层厚度检测设备", //设备类型
                fNote: "2",
                modelType: "2", // 0代办 1已办
                sublevel: 0, //查看下级  1是0否
            })
                .then((res) => {
                    if (res.errcode == 0) {
                        this.formObj[8].options = [];
                        res.data.mangEquipmentList.list.map(item => {
                            this.formObj[8].options.push({
                                label: item.fEqptname + "/" + item.fEqptno,
                                value: item.fId
                                // value: item.fEqptname + "/" + item.fEqptno

                            })
                        })
                    } else {
                        this.$message.error(res.msg);
                        return false;
                    }
                })
                .catch(() => {
                    return false;
                });
        },
        yj_saveForm() {
            var save = {};
            save = {
                fId: this.formData.fId, // 申请单fid //为空判断为新增
                fWitnessid: this.formData.fWitnessid, //委托单id
                fWitnessno: this.formData.fWitnessno,//委托单编号
                fEngineeringname: this.formData.fEngineeringname, // 工程名称
                fEngineeringid: this.formData.fEngineeringid, // 工程id
                fEngineeringpart: this.formData.fEngineeringpart, // 工程部位
                fEngineeringpartid: this.formData.fEngineeringpartid, // 工程部位id
                fPlantestdates: this.formData.fPlantestdates, // 计划检测日期 年-月-日
                fTestcondition: this.formData.fTestcondition, // 试验条件
                fTestbasisid: this.formData.fTestbasisid, // 检测依据id
                fTestbasisname: this.formData.fTestbasisname, // 检测依据内容
                fDecidebasisid: this.formData.fDecidebasisid, // 判定依据id
                fDecidebasisname: this.formData.fDecidebasisname, // 判定依据内容
                fEquipnameandno: this.formData.fEquipnameandno, // 检测仪器设备名称及编号
                // fEquipnameandnoid: this.formData.fEquipnameandnoid, // 检测仪器设备名称及编号id
                fThicknessdesignvalue: this.formData.fThicknessdesignvalue, // 保护层厚度设计值(mm)
                fAllowdeviation: this.formData.fAllowdeviation, // 允许偏差(+mm)
                fAllowdeviation1: this.formData.fAllowdeviation1, // 允许偏差(-mm)
                fCorrectionfactor: this.formData.fCorrectionfactor, // 修正因子(mm)*
                fAuthorid: this.formData.fAuthorid, // 编制人ID
                fAuthorname: this.formData.fAuthorname, // 编制人名称
                companyId: this.formData.fCompanyid, // 当前用户标段ID
                companyName: this.formData.fCompanyname, // 当前用户标段名称
                supervisionId: this.userInfo.jlCompanyId, // 当前用户监理ID
                supervisionName: this.userInfo.jlCompanyName, // 当前用户监理名称
                projectId: this.userInfo.projectId, // 当前用户项目ID
                projectName: this.userInfo.projectName, // 当前用户项目名称
                userId: this.userInfo.userId, // 当前登录人id
                userName: this.userInfo.realName, // 当前登录人姓名
                fNature: this.userInfo.companyNature//当前用户的机构性质
            };
            if (this.$refs.publicForm.submitForm()) {
                this.isLock = true;
                saveSteelProtectionTestTask(save)
                    .then((res) => {
                        if (res.errcode == 0) {
                            this.visible = false;
                            this.$emit("refreshList");
                            this.$message.success("保存成功");
                            this.isLock = false
                        } else {
                            this.isLock = false
                            this.$message.error(res.msg);
                            console.log("保存接口报错");
                        }
                    })
                    .catch((error) => {
                        console.log("保存接口", error);
                    });
            } else {
                console.log("error");
            }
        },
        // 时间
        getData() {
            var time = new Date();
            const y = time.getFullYear();
            const m = (time.getMonth() + 1 + "").padStart(2, "0");
            const d = (time.getDate() + "").padStart(2, "0");
            const hh = (time.getHours() + "").padStart(2, "0");
            const mm = (time.getMinutes() + "").padStart(2, "0");
            const ss = (time.getSeconds() + "").padStart(2, "0");
            this.timeYMD = `${y}-${m}-${d}`;
            this.timeYmdHms = `${y}-${m}-${d} ${hh}:${mm}:${ss}`;
        },
        // WBS
        refreshList() { },
        WBSDialog() { // WBS树关闭
            this.WBSShow = false;
        },
        getWBS(data) { //获取选择WBS信息
            this.formData.fEngineeringpart = data.fLongname //工程部位
            this.formData.fEngineeringpartid = data.fNodeid //工程部位id
        },
        // WBS


        // 获取委托单列表数据
        closeDialog1() {
            this.isShow = false;
        },
        getEntrustRow(data) {
            // console.log("获取委托单列表数据", data);
            this.formData.fCompanyname = data.fCompanyname;//标段名称--施工单位
            this.formData.fCompanyid = data.fCompanyid;//标段名称--标段ID
            this.formData.fEngineeringname = data.fProjectuse;//标段名称--工程名称
            this.formData.fEngineeringid = "";//标段名称--工程名称id
            this.formData.fEngineeringpart = data.fProjecttypename;//工程部位名称
            this.formData.fEngineeringpartid = data.fProjecttypeid;//工程部位ID
            this.formData.fThicknessdesignvalue = data.fThicknessdesignvalue;//保护层厚度设计值
            this.formData.fAllowdeviation = data.fAllowdeviation;//允许偏差+
            this.formData.fAllowdeviation1 = data.fAllowdeviation1;//允许偏差-
            this.formData.fTestbasisid = "";//检测依据ID
            this.formData.fTestbasisname = data.fTestnote;//检测依据内容
            this.formData.fDecidebasisid = "";//判定依据ID
            this.formData.fDecidebasisname = data.fJudgetype;//判定依据名称
            this.formData.fWitnessno = data.fEntrustno;//委托单编号
            this.formData.fWitnessid = data.fId;//委托单id
            // 多选
            // 检测依据
            if (data.fTestnote != '') {
                var fTestnoteList = []
                this.formObj[6].options.map(item => {
                    data.fTestnote.split(",").map(i => {
                        if (item.label == i) {
                            fTestnoteList.push(item)
                        }
                    })
                })
            } else {
                fTestnoteList = []
            }
            // 判定依据
            if (data.fJudgetype != '') {
                var fJudgetypeList = []
                this.formObj[7].options.map(item => {
                    data.fJudgetype.split(",").map(i => {
                        if (item.label == i) {
                            fJudgetypeList.push(item)
                        }
                    })
                })
            } else {
                fJudgetypeList = []
            }

            this.selected = [
                {
                    name: "fTestbasisnameList",
                    selected: fTestnoteList,
                },
                {
                    name: "fDecidebasisnameList",
                    selected: fJudgetypeList,
                }
            ];
            // 多选

        },
        // 获取委托单列表数据

    },
};